[php]有一段语句不懂求解释一下

来源:百度知道 编辑:UC知道 时间:2024/05/15 08:55:33
//分类函数
function select_option($selected=0){
global $db;
$sql="select * from goods_type";
$result=mysql_query($sql) or die($sql."有错误");
while($row=mysql_fetch_assoc($result)){
$string.="<option value=\"".$row["ty_id"]."\" ";
if($row["ty_id"]==$selected) $string.="selected=\"selected\"";//控制初始化选定结果
$string.=">";
$string.=$row["ty_name"];
$string.="</option> \n";
}
return $string;
}
没看懂

function select_option($selected=0){ // 定义select_option函数
global $db;
$sql="select * from goods_type"; // SQL语句
$result=mysql_query($sql) or die($sql."有错误"); // 执行SQL语句
while($row=mysql_fetch_assoc($result)){ // 得到记录
$string.="<option value=\"".$row["ty_id"]."\" ";
if($row["ty_id"]==$selected) $string.="selected=\"selected\"";//控制初始化选定结果
$string.=">";
$string.=$row["ty_name"];
$string.="</option> \n";
}
return $string;
}

这是个自定义函数代码
定义select_option()函数
从数据库中查询出goods_type的所有记录
生成下拉菜单。当ty_id = $selected时默认选中。